import 'package:flutter/material.dart';
import '../blocs/bloc_provider.dart';
import '../view/view_mainrole.dart';
import '../view/view_otherrole.dart';
import '../model/playing_card.dart';
import '../components/static_playingcard.dart';
import '../blocs/doudizhu_room_bloc.dart';
import '../model/other_role.dart';

class PageRoomDoudizhu extends StatelessWidget {
  DoudizhuRoomBloc doudizhuRoomBloc;
  final int antes;
  PageRoomDoudizhu({this.antes});

  @override
  Widget build(BuildContext context) {
    doudizhuRoomBloc = DoudizhuRoomBloc(roomContext: context);
    return BlocProvider<DoudizhuRoomBloc>(
      bloc: this.doudizhuRoomBloc,
      child: _ContentFrame(antes: antes)
    );
  }
}
class _ContentFrame extends StatelessWidget {
  DoudizhuRoomBloc doudizhuRoomBloc;
  //用来保存创建的otherRole，否则每次otherOutRoom都会让其它otherRole重绘，就必须把其中的Stream设置为broadcast
  Map<String, Widget> otherRolesWidgetCache = {};

  Widget roomContent;

  final int antes;
  _ContentFrame({this.antes});

  Widget getOtherRoles(BuildContext context) {
    return StreamBuilder<Map<String, OtherRole>>(
      stream: this.doudizhuRoomBloc.otherRolesStream,
      initialData: this.doudizhuRoomBloc.otherRoleData,
      builder: (BuildContext context, AsyncSnapshot<Map<String, OtherRole>> snapshot) {
        List<Widget> otherRoles = [];
        List<OtherRole> otherRoleList = snapshot.data.values.toList();
        otherRoleList.sort((otherRole1, otherRole2){
          return otherRole1.disToMainRole == -otherRole2.disToMainRole ? otherRole1.disToMainRole - otherRole2.disToMainRole : otherRole2.disToMainRole - otherRole1.disToMainRole;
        });
        OtherRole otherRole;
        String otherRoleId;
        ViewOtherrole viewOtherrole;
        for(int index = 0; index < otherRoleList.length; index ++ ) {
          otherRole = otherRoleList[index];
          otherRoleId = otherRole.roleId;
          
          if(!otherRolesWidgetCache.containsKey(otherRoleId)) {
            viewOtherrole = ViewOtherrole<DoudizhuRoomBloc>(roleId:otherRoleId);
            if(index % 2 != 0 || (otherRoleList.length == 1 && otherRole.disToMainRole > 0)) {
              viewOtherrole.setPos(true);
            }
            otherRolesWidgetCache[otherRoleId] = viewOtherrole;
          }
          otherRoles.add(otherRolesWidgetCache[otherRoleId]);
        }
        
        if(otherRoles.length > 0) {
          if(otherRoles.length == 1 && (otherRoles[0] as ViewOtherrole).isRight) {
            otherRoles.insert(0, Expanded(child: Container()));
          }
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: otherRoles,
          );
        }else {
          return Container();
        }
      }
    );
  }

  Widget getLandlord() {
    return StreamBuilder<List>(
      stream: this.doudizhuRoomBloc.landlordStream,
      initialData: this.doudizhuRoomBloc.getLandlordData(),
      builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
        List<Widget> landlordWidgets = [];
        int cardIndex = 0;
        snapshot.data.forEach((landlordCard){
          if(cardIndex++ != 0) {
            landlordWidgets.add(Container(width: 4));
          }
          landlordWidgets.add(StaticPlayingCard(PlayingCard(cardText: landlordCard,sizeType: 3)));
        });
        return Container(
          margin: EdgeInsets.only(top: 5),
          height: PlayingCard.cardHeight3,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: landlordWidgets,
          ),
        );
      }
    );
  }
  Widget getMagnification() {
    return StreamBuilder<int>(
      stream: this.doudizhuRoomBloc.magnificationStream,
      initialData: this.doudizhuRoomBloc.getMagnificationData(),
      builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
        return Container(
          padding: EdgeInsets.symmetric(vertical: 5),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('底注：'+this.antes.toString(), style: TextStyle(color: Color.fromARGB(200, 219,98,3),fontSize: 15)),
              Container(width: 18,),
              Text('倍数：x '+snapshot.data.toString(), style: TextStyle(color: Color.fromARGB(200, 219,98,3),fontSize: 15))
            ],
          ),
        );
      },
    );
  }
  
  @override
  Widget build(BuildContext context) {
    this.doudizhuRoomBloc = BlocProvider.of<DoudizhuRoomBloc>(context);
    return Column(
      children: <Widget>[
        this.getLandlord(),
        this.getMagnification(),
        Expanded(
          flex: 1,
          child: this.getOtherRoles(context)
        ),
        ViewMainRole<DoudizhuRoomBloc>()
      ]
    );
  }
}